package com.homer.gps.track;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.homer.gps.track.adapters.RoutesArrayAdapter;
import com.homer.gps.track.persist.DatabaseHelper;
import com.homer.gps.track.persist.Repository;
import com.homer.gps.track.persist.RouteModel;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public class RoutesActivity extends OrmLiteBaseActivity<DatabaseHelper> {
	
	private ListView routesList;

	private RoutesArrayAdapter routesListAdapter;
	private Repository repository;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_routes);
		
		this.repository = new Repository(getHelper());
		this.routesListAdapter = new RoutesArrayAdapter(this, R.layout.routes_list_item, repository.getRoutes());
		
		routesList = (ListView) findViewById(R.id.routes_list);
		routesList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg) {
				RouteModel route = RoutesActivity.this.routesListAdapter.getItem(position);
				//RoutesActivity.this.repository.deleteRoute(route);
				//updateList();
				//Log.e("******list count", String.valueOf(routesListAdapter.getCount()));
				Intent intent = new Intent(RoutesActivity.this, RouteDetail.class);
				intent.putExtra("route_position", position);
			    startActivity(intent);
			} 
		});
		this.routesList.setAdapter(this.routesListAdapter);
	}
	
	public void updateList(){
		routesListAdapter.clear();
		for(RouteModel route : repository.getRoutes()){
			routesListAdapter.add(route);
		}
	}

}
